package com.example.statistics.ui.purchase

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.ColdStorageBean
import com.example.statistics.bean.StorageBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.*
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.ex.afterTextChangedListener
import kotlinx.android.synthetic.main.activity_storage_fee_edit.*

class StorageFeeEditActivity : BaseActivity() {

    override val title: String
        get() = "仓储费"


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_storage_fee_edit)
        tv_start_date.contentText = intent.getStringExtra(Constants.START_DATE)?.goTimeFormat()
        tv_end_date.contentText = intent.getStringExtra(Constants.END_DATE)?.goTimeFormat()
        tv_storage.contentText = intent.getStringExtra(Constants.STORAGE)
        val containerNo = intent.getStringExtra(Constants.CONTAINER_NO)
        val purchaseId = intent.getIntExtra(Constants.PURCHASE_PRODUCT_ID,0)
        val weight = intent.getDoubleExtra(Constants.WEIGHT, 0.0) ?: 0.0
        val city = intent.getStringExtra(Constants.CITY)

        tv_product_weight.contentText = weight.toString()
        tv_storage_days.contentText = daysDifference(tv_start_date.contentText, tv_end_date.contentText).toString()

        tv_product_weight.afterTextChangedListener { _, _ ->
            feeChanged()
        }
        tv_unit_price.afterTextChangedListener { _, _ ->
            feeChanged()
        }
        tv_start_date.onDateClicked {
            tv_storage_days.contentText = daysDifference(it, tv_end_date.contentText).toString()
            feeChanged()
        }
        tv_end_date.onDateClicked {
            tv_storage_days.contentText = daysDifference(tv_start_date.contentText, it).toString()
            feeChanged()
        }
        tv_storage.onContentClicked {
            httpGet<List<ColdStorageBean>>(UrlConfig.PURCHASE_STORAGE_LIST, config = {
                listParams = listOf("city" to city)
                isShowDialog = true
            }) {
                singleDialog(it) { s ->
                    tv_storage.contentText = s.storage
                    tv_unit_price.contentText = s.price.toString()
                    feeChanged()
                }
            }
        }


        httpGet<ColdStorageBean>(UrlConfig.PURCHASE_STORAGE_DATA, config = {
            listParams = listOf("storage" to tv_storage.contentText)
            isShowDialog = true
        }) {
            tv_unit_price.contentText = it?.price.toString()
            feeChanged()
        }

        topBarRightText("保存") {
            val id = intent.getIntExtra(Constants.ID, 0)
            val url = if (id == 0) UrlConfig.PURCHASE_STORAGE_FEE_ADD else UrlConfig.PURCHASE_STORAGE_FEE_UPDATE
            httpPost<StorageBean>(url, config = {
                listParams = listOf(
                        "ID" to id,
                        "purchaseProductId" to purchaseId,
                        "containerNo" to containerNo,
                        "storage" to tv_storage.contentText,
                        "weight" to tv_product_weight.contentText,
                        "unitPrice" to tv_unit_price.contentText,
                        "startDate" to tv_start_date.contentText,
                        "endDate" to tv_end_date.contentText,
                        "storageDays" to tv_storage_days.contentText,
                        "storageFee" to tv_storage_fee.contentText
                )
                isSuccessToast = true
            }) {
                activityFinish()
            }
        }
    }

    private fun feeChanged() {
        tv_storage_fee.contentText = tv_storage_days?.contentText?.toIntOrNull()
                ?.mulEx(tv_unit_price.contentText?.toDoubleOrNull())
                ?.mulEx(tv_product_weight.contentText?.toDoubleOrNull())
                ?.divEx(1000).toString()
    }

}